package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {


    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private EmpExprMapper empExprMapper;


    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {

        //创建分页模型对象，设置页码和每页数量
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        //添加排序条件，根据最后修改时间倒序
        page.addOrder(OrderItem.desc("update_time"));
        //封装结果并返回
        Page<Emp> empPage = page(page);

        //处理部门名称
        List<Emp> list = empPage.getRecords();
        //拿到列表数据里面所有的不重复的id列表
        Set<Integer> deptIds = list.stream().map(emp -> emp.getDeptId()).collect(Collectors.toSet());
        //根据ids列表，调一次数据库拿到name，做成map
        List<Dept> deptList = deptService.listByIds(deptIds);
        Map<Integer, String> map = deptList.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));

        for (Emp emp : list) {
            Integer deptId = emp.getDeptId();
            emp.setDeptName(map.get(deptId));
        }


        return new PageResult<>(empPage.getTotal(),empPage.getRecords());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        save(emp);


        List<EmpExpr> exprList = emp.getExprList();
        if (exprList!=null && !exprList.isEmpty()){
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
            empExprService.saveBatch(exprList);
        }



    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);

        LambdaQueryWrapper<EmpExpr> wrapper = Wrappers.<EmpExpr>lambdaQuery()
                .eq(EmpExpr::getEmpId, id);

        List<EmpExpr>  exprs = empExprService.list(wrapper);
        emp.setExprList(exprs);
        return emp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        //调Service层方法修改员工信息
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);

        //修改员工工作经历
       // removeById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
    }
}
